﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApiV1.Domain;
using WebApiV3.Data.Entity;
using WebApiV3.Domoin.Enum;
using WebApiV3.Interface.Log;

namespace WebApiV3.Implement.Log
{
    public class Log : ILog
    {
        private readonly IRespository<Logs> _logsRespository;

        public Log(IRespository<Logs> logsRespository)
        {
            _logsRespository = logsRespository;
        }
        public void Clear()
        {
            var list = _logsRespository.Table.ToList();

            _logsRespository.DeletedBulk(list);
        }

        public void Delete(Logs log)
        {
            _logsRespository.Delete(log);
        }

        public Logs GetLogById(int id)
        {
            return _logsRespository.GetById(id);
        }

        public void Insert(Logs log)
        {
            _logsRespository.Insert(log);
        }

        public IEnumerable<Logs> MultiQuery(DateTime beginTime, DateTime endTime, Loglevel logLevel, int userId = 0)
        {
            var bTime = beginTime == null ? DateTime.Now : beginTime;
            var eTime = endTime == null ? DateTime.Now : endTime;


            var list = _logsRespository.Table.Where(x => x.CreatedTime >= bTime && x.CreatedTime <= eTime && x.LogLevel == (int)logLevel);

            if (userId != 0)
            {
                list = list.Where(x => x.UserId == userId);
            }
            return list;
        }
    }
}
